package com.vornitologist.ui;

import java.util.List;
import java.util.ResourceBundle;

import com.guruyu.hawaii.core.data.TaskData;
import com.guruyu.hawaii.interfaces.HawaiiFactory;
import com.vaadin.addon.touchkit.ui.NavigationView;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Table;

public class MyTask extends NavigationView implements ClickListener {

	private Button exit = new Button(null, this);

	private ResourceBundle tr;

	private Table table = new Table();

	@Override
	public void attach() {
		super.attach();
		if (tr == null) {
			buildView();
		}
	}

	private void buildView() {

		setCaption("Mis Tareas");
		table.setSizeFull();
		populateTable();

		table.setSelectable(true);
		setContent(table);

		exit.setIcon(new ThemeResource("linegraphics/home.png"));
		setRightComponent(exit);

		table.addListener(new Table.ValueChangeListener() {
			public void valueChange(ValueChangeEvent event) {
				if (table.getValue() != null) {
					getNavigationManager().navigateTo(
							new CompleteTaskView(table.getValue()));
				}
			}
        });
        
        table.addListener(new ItemClickEvent.ItemClickListener() {
			public void itemClick(ItemClickEvent event) {
				
				}
			});

	}

	private void populateTable() {
		List<TaskData> list = HawaiiFactory.getInstance().getITaskController()
				.getTaskListByUser((String) this.getApplication().getUser());
		BeanItemContainer<TaskData> dataSource = new BeanItemContainer<TaskData>(
				TaskData.class, list);

		table.setContainerDataSource(dataSource);
		table.setVisibleColumns(new String[] { "id", "nombre", "proceso" });
	}

	public void buttonClick(ClickEvent event) {
		if (exit == event.getButton()) {
			getWindow().setContent(new LoginView(getWindow()));
		} else {
		}
	}

}
